package com.ibiaoke.order.service;

import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.order.dao.ShoppingCartGoodsDao;
import com.ibiaoke.order.entity.ShoppingCart;
import com.ibiaoke.order.entity.ShoppingCartGoods;
import com.ibiaoke.order.entity.ShoppingCartGoodsExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Dragon
 * @date 2018/5/17/11:41;
 */
@Service
public class ShoppingCartGoodsServiceImpl implements ShoppingCartGoodsService {

    @Autowired
    private ShoppingCartGoodsDao shoppingCartGoodsDao;

    @Override
    public ShoppingCartGoods getGoodsInfos(Integer cartId, Integer goodsId, Integer shopId) {
        ShoppingCartGoodsExample example = new ShoppingCartGoodsExample();
        example.createCriteria().andShoppingcartIdEqualTo(cartId).andGoodsIdEqualTo(goodsId).andShopIdEqualTo(shopId).andDelStatusEqualTo((byte) CommonConsts.NO);
        return shoppingCartGoodsDao.getByExample(example);
    }

    @Override
    public Integer updateShoppingCartGoods(Integer cartId, Integer goodsId, Integer shopId, Integer num) {
        AssertUtils.notNull(cartId,"购物车Id不能为空");
        AssertUtils.notNull(shopId,"店铺Id不能为空");
        AssertUtils.notNull(num,"商品数量不能为空");
        ShoppingCartGoodsExample example = new ShoppingCartGoodsExample();
        example.createCriteria().andShoppingcartIdEqualTo(cartId).andShopIdEqualTo(shopId);
        ShoppingCartGoods cartGoods = new ShoppingCartGoods();
        cartGoods.setNum(num);
        return shoppingCartGoodsDao.updateByExampleSelective(cartGoods,example);
    }

    @Override
    public Integer saveNewInfo(ShoppingCartGoods cartGoods) {
        AssertUtils.notNull(cartGoods,"数据填写异常");
        return shoppingCartGoodsDao.insertSelective(cartGoods);
    }

    @Override
    public int deleteShopppingCartGoods(Integer cartId) {
        ShoppingCartGoodsExample example = new ShoppingCartGoodsExample();
        example.createCriteria().andShoppingcartIdEqualTo(cartId);

        ShoppingCartGoods shoppingCartGoods = new ShoppingCartGoods();
        shoppingCartGoods.setDelStatus((byte) CommonConsts.YES);
        return shoppingCartGoodsDao.updateByExampleSelective(shoppingCartGoods,example);
    }

    @Override
    public List<ShoppingCartGoods> listCartGoods(List<Integer> cartIds, List<Integer> shopIds) {

        return shoppingCartGoodsDao.listCartGoods(cartIds,shopIds);
    }

    @Override
    public Integer updateNum(Integer cartId, Integer goodsId, Integer shopId, Integer num) {
        ShoppingCartGoodsExample example = new ShoppingCartGoodsExample();
        example.createCriteria().andShoppingcartIdEqualTo(cartId).andGoodsIdEqualTo(goodsId).andShopIdEqualTo(shopId);
        ShoppingCartGoods shoppingCartGoods = new ShoppingCartGoods();
        shoppingCartGoods.setNum(num);
        return shoppingCartGoodsDao.updateByExampleSelective(shoppingCartGoods,example);
    }

    @Override
    public List<ShoppingCartGoods> getShoppingCartByCartId(List<Integer> ids) {
        AssertUtils.notNull(ids, "数据异常");
        return shoppingCartGoodsDao.getShoppingCartByCartId(ids);
    }

}
